package com.jonluo.service.listener;


import com.jonluo.service.registry.ServiceRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
@Component
public class WebListener implements ServletContextListener {

  @Value("${server.address}")
  private String serverAddress;

  @Value("${server.port}")
  private int serverPort;

  private final ServiceRegistry serviceRegistry;

  @Autowired
  public WebListener(ServiceRegistry serviceRegistry) {
    this.serviceRegistry = serviceRegistry;
  }

  @Override
  public void contextInitialized(ServletContextEvent event) {
    // 注册服务
    serviceRegistry.register(String.format("%s:%d", serverAddress, serverPort));
  }

  @Override
  public void contextDestroyed(ServletContextEvent servletContextEvent) {
  }
}
